/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.modules.autocode.util;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.ksplatform.modules.autocode.dto.Column;
import cn.com.ksplatform.modules.autocode.dto.ColumnArray;
import cn.com.ksplatform.modules.autocode.dto.Propertys;
import cn.com.ksplatform.modules.autocode.global.Config;
import cn.com.ksplatform.modules.autocode.manager.model.Module;
import cn.com.ksplatform.modules.autocode.manager.model.Sence;

/**
 * ClassName:SenceUtil.java <br/>
 * Date: 2015年12月30日 <br/>
 * 
 * @author Mr.Xiao
 * @version 1.0
 */
public class SenceUtil {
	/**
	 * 获取字段
	 * @param senceModel
	 * @return
	 * @throws Exception
	 */
	public static ColumnArray getColumns(Sence senceModel) throws Exception{
		return getColumns(senceModel,"Columns.xml");
	}
	
	/**
	 * 获取字段
	 * @param senceModel
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static ColumnArray getColumns(Sence senceModel,String fileName) throws Exception{
		Module moduleModle = senceModel.getModule();
		String columnFilePath = Config.PATH+File.separator+moduleModle.getModulesCode()+File.separator+senceModel.getSenceCode()+File.separator+senceModel.getSenceVersion()+File.separator+fileName;
		File columnFile = new File(columnFilePath);
		if(!columnFile.exists()){
			throw new Exception("场景版本不存在!!! Version:"+senceModel.getSenceVersion());
		}
		ColumnArray columns = (ColumnArray) XmlUtil.getObjectFromXml(ColumnArray.class, new FileInputStream(columnFile));
		return columns;
	}
	
	/**
	 * 获取属性
	 * @param senceModel
	 * @return
	 * @throws Exception
	 */
	public static Propertys getPropertys(Sence senceModel) throws Exception{
		return getPropertys(senceModel,"Propertys.xml");
	}
	/**
	 * 获取属性
	 * @param senceModel
	 * @return
	 * @throws Exception
	 */
	public static Propertys getPropertys(Sence senceModel,String fileName) throws Exception{
		String path = Config.PATH+File.separator+senceModel.getModule().getModulesCode()+File.separator+senceModel.getSenceCode()+File.separator+senceModel.getSenceVersion();
		File f = new File(path);
		if(f.exists()){
			Propertys propertys = (Propertys) XmlUtil.getObjectFromXml(Propertys.class, new FileInputStream(f.getPath()+File.separator+fileName));
			return propertys;
		}else{
			throw new Exception("版本不存在");
		}
	}
	
	/**
	 * 对象字段分类
	 * @param columnArray
	 * @return
	 */
	public static Map ClassifyColumns(ColumnArray columnArray){
		List<Column> querys = new ArrayList<Column>();
		List<Column> lists = new ArrayList<Column>();
		List<Column> pks = new ArrayList<Column>();
		List<Column> forms = new ArrayList<Column>();
		for(Column c : columnArray.getColumns()){
			if(c.getPropertys() != null){
				if("true".equals(c.getPropertys().get("query"))){
					//查询条件
					querys.add(c);
				}
				
				if("true".equals(c.getPropertys().get("list")) && !c.getPk()){
					//列表显示
					lists.add(c);
				}
				
				if(c.getPk()){
					//主键
					pks.add(c);
				}
				
				if("true".equals(c.getPropertys().get("formShow")) && !c.getPk()){
					forms.add(c);
				}
			}
		}
		Map m = new HashMap();
		m.put("query", querys);
		m.put("form", forms);
		m.put("list", lists);
		m.put("pk", pks);
		m.put("columns", columnArray.getColumns());
		return m;
	}
}
